<?php
// File: /path/to/makeindex.php
$dbh = mysql_connect('localhost', 'root', '');
mysql_select_db('testweb');
$result = mysql_query("SELECT * FROM members");
while($member = mysql_fetch_assoc($result)) {
	$data[$member['id']]['member'] = $member;
	$addressResult = mysql_query("SELECT * FROM addresses WHERE member_id = '{$member['id']}'");
	$data[$member['id']]['addresses'] = array();
	while($address = mysql_fetch_assoc($addressResult)) {
		$data[$member['id']]['addresses'][] = $address;
	}
}
$xmlwriter = new xmlWriter;
$xmlwriter->openMemory();
$xmlwriter->setIndent(true);
$xmlwriter->startDocument('1.0', 'UTF-8');
$xmlwriter->startElement('sphinx:docset');
$xmlwriter->startElement('sphinx:schema');
$xmlwriter->startElement('sphinx:field');
$xmlwriter->writeAttribute("name", "name");
$xmlwriter->endElement();
$xmlwriter->startElement('sphinx:field');
$xmlwriter->writeAttribute("name", "addresses");
$xmlwriter->endElement();
$xmlwriter->endElement();
foreach($data as $id => $member) {
	$xmlwriter->startElement('sphinx:document');
	$xmlwriter->writeAttribute("id", $id);
	$xmlwriter->startElement('name');
	$xmlwriter->text($member['member']['name']);
	$xmlwriter->endElement();
	$xmlwriter->startElement('addresses');
	$buffer = '';
	foreach($member['addresses'] as $address) {
		$buffer .= $address['address'] . ' ' . $address['city'] . ' ' . $address['zipcode'] . ' ';
	}
	$xmlwriter->text($buffer);
	$xmlwriter->endElement();
	$xmlwriter->endElement();
}
$xmlwriter->endElement();
print $xmlwriter->flush();

?>